package com.redwine.app.view;

import android.content.Context;
import android.graphics.Color;
import android.graphics.drawable.ColorDrawable;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.BaseAdapter;
import android.widget.PopupWindow;
import android.widget.TextView;

import com.redwine.app.R;

import java.util.ArrayList;
import java.util.List;


/**
 * 我发布采购的选择框
 * 作者：gassion on 17/9/10 11:59
 * 最可怕的敌人，就是没有坚强的信念。
 */
public class PopupListWindow extends PopupWindow {

    private Context context;
    private OnClickListener onClickListener;
    private List<String> mDatas = new ArrayList<>();

    public PopupListWindow(Context context, List<String> datas) {
        this.context = context;
        this.mDatas = datas;
        initView();
    }

    public void setOnClickListener(OnClickListener onClickListener) {
        this.onClickListener = onClickListener;
    }

    private void initView() {
        View contentView = LayoutInflater.from(context).inflate(R.layout.popu_window, null);
        setWidth(ViewGroup.LayoutParams.WRAP_CONTENT);
        setHeight(ViewGroup.LayoutParams.WRAP_CONTENT);
        setFocusable(true);
        setOutsideTouchable(true);
        ColorDrawable dw = new ColorDrawable(Color.TRANSPARENT);
        setBackgroundDrawable(dw);
        MeasureWidthListView measureListView = (MeasureWidthListView) contentView.findViewById(R.id.popu_measure_list_view);
        ListViewAdapter listViewAdapter = new ListViewAdapter();
        measureListView.setAdapter(listViewAdapter);
        measureListView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
            @Override
            public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
                dismissPopupWindow();
                if (null != onClickListener)
                    onClickListener.onClick(position);
            }
        });
        setContentView(contentView);
    }

    private void dismissPopupWindow() {
        if (!isShowing()) return;
        dismiss();
    }

    public interface OnClickListener {
        void onClick(int position);
    }

    private class ListViewAdapter extends BaseAdapter {

        @Override
        public int getCount() {
            return mDatas.size();
        }

        @Override
        public Object getItem(int position) {
            return mDatas.get(position);
        }

        @Override
        public long getItemId(int position) {
            return position;
        }

        @Override
        public View getView(int position, View convertView, ViewGroup parent) {
            LayoutInflater inflater = LayoutInflater.from(context);
            View view = inflater.inflate(R.layout.popu_window_item, parent, false);
            TextView tvName = (TextView) view.findViewById(R.id.popu_item_tv_name);
            tvName.setText(mDatas.get(position));
            View line = view.findViewById(R.id.popu_item_line);
            if (position == mDatas.size() - 1) {
                line.setVisibility(View.GONE);
            } else {
                line.setVisibility(View.VISIBLE);
            }
            return view;
        }
    }

}
